﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using nyxwin.Main.Base.ViewModels;
using nyxwin.Data.Mail;
using nyxwin.Common.Silverlight.Commands;
using System.Windows.Media.Imaging;

namespace nyxwin.Main.ViewModel.SubModels
{
    public class MailModel : BaseViewModel
    {
        private string _direction;
        private Command _filterByNickCommand;
        private DateTime _time;

        protected override void OnLocalizationChanged()
        {
            base.OnLocalizationChanged();

            RaisePropertyChanged(() => Direction);
            RaisePropertyChanged(() => DisplayTime);
        }

        public override void Unload()
        {
            base.Unload();

            if (Active != null)
                Active.Unload();
        }

        public MailModel(Mail m, Command filterByNickCommand)
        {
            this.Id = m.Id;
            this.Nick = m.Nick;
            this._direction = m.Direction;
            this.Content = m.Content;
            this.NickImageUrl = m.NickImageUrl;
            this._time = m.DisplayTime;

            if (m.Active != null)
                this.Active = new ActiveViewModel(m.Active);
            else
                this.Active = null;

            this.ReplyCommand = new Command(x => Navigate(new Uri("/View/SubViews/NewMail.xaml?nick=" + this.Nick, UriKind.Relative)));
            this._filterByNickCommand = filterByNickCommand;
            this.FilterByNickCommand = new Command(x =>
            {
                _filterByNickCommand.Execute(this.Nick);
            });

            this.Unread = m.Unread;
            this.UnreadOther = m.UnreadOther;
        }

        public string Nick { get; private set; }
        public int Id { get; private set; }
        public string Content { get; private set; }
        public string Direction { get { return Localization.GetString("MailDirection_" + _direction); } }
        public Uri DirectionImage { get { return (RawDirection == "from") ? new Uri("/Icons/next.png", UriKind.Relative) : new Uri("/Icons/back.png", UriKind.Relative); } }
        public string RawDirection { get { return _direction; } }
        public Uri NickImageUrl { get; private set; }
        public bool Unread { get; private set; }
        public bool UnreadOther { get; private set; }

        public string DisplayTime { get { return _time.ToString(); } }

        public ActiveViewModel Active { get; private set; }
        public Visibility ActiveVisibility { get { return (Active != null) ? Visibility.Visible : Visibility.Collapsed; } }

        public Command ReplyCommand { get; private set; }
        public Command FilterByNickCommand { get; private set; }
    }
}
